//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MakeYoYInflationCapFloor.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Indexes/YoYInflationIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Instruments/YoYInflationCapFloor.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CMakeYoYInflationCapFloor::CMakeYoYInflationCapFloor (QL::Instruments::YoYInflationCapFloor::TypeEnum capFloorType, UInt64 length, Cephei::QL::Times::ICalendar^ cal, Cephei::QL::Indexes::IYoYInflationIndex^ index, Cephei::QL::Times::IPeriod^ observationLag, Microsoft::FSharp::Core::FSharpOption<Double>^ strike, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ forwardStart) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCalendar^ _Ccal;
    CYoYInflationIndex^ _Cindex;
    CPeriod^ _CobservationLag;
    CPeriod^ _CforwardStart;
    try
    {
#ifdef HANDLE
        _phMakeYoYInflationCapFloor = NULL;
#endif
        QuantLib::YoYInflationCapFloor::Type _capFloorType = (QuantLib::YoYInflationCapFloor::Type)capFloorType ;
        QuantLib::Size _length = (QuantLib::Size)ValueHelper::Convert (length);
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        _Cindex = safe_cast<CYoYInflationIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::YoYInflationIndex>& _index = static_cast<boost::shared_ptr<QuantLib::YoYInflationIndex>&> (_Cindex->GetShared ()); 
        _CobservationLag = safe_cast<CPeriod^> (observationLag);
        _CobservationLag->Lock();
        QuantLib::Period& _observationLag = static_cast<QuantLib::Period&> (_CobservationLag->GetReference ()); 
        QuantLib::Rate _strike = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (strike) ? (QuantLib::Rate)ValueHelper::Convert (strike->Value) : Null<QuantLib::Rate>()); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (forwardStart))
        {
            _CforwardStart = safe_cast<CPeriod^> (forwardStart->Value);
            _CforwardStart->Lock();
        }
        QuantLib::Period& _forwardStart = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>::IsSome::get (forwardStart) ? static_cast<QuantLib::Period&> (_CforwardStart->GetReference ()) : 0*Days); //1
        _ppMakeYoYInflationCapFloor = new boost::shared_ptr<QuantLib::MakeYoYInflationCapFloor> (new QuantLib::MakeYoYInflationCapFloor ( _capFloorType,  _length,  _cal,  _index,  _observationLag,  _strike,  _forwardStart ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CobservationLag != nullptr) _CobservationLag->Unlock();
        if (_CforwardStart != nullptr) _CforwardStart->Unlock();
    }
}
Cephei::QL::Instruments::CMakeYoYInflationCapFloor::CMakeYoYInflationCapFloor (boost::shared_ptr<QuantLib::MakeYoYInflationCapFloor>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeYoYInflationCapFloor = NULL;
#endif
	_ppMakeYoYInflationCapFloor = &childNative;
    
}
Cephei::QL::Instruments::CMakeYoYInflationCapFloor::CMakeYoYInflationCapFloor (QuantLib::MakeYoYInflationCapFloor& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMakeYoYInflationCapFloor = NULL;
#endif
	_ppMakeYoYInflationCapFloor = new boost::shared_ptr<QuantLib::MakeYoYInflationCapFloor> (&childNative);
    
    _MakeYoYInflationCapFloorOwner = owner;
    
}

Cephei::QL::Instruments::CMakeYoYInflationCapFloor::CMakeYoYInflationCapFloor (CMakeYoYInflationCapFloor^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeYoYInflationCapFloor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMakeYoYInflationCapFloor = new boost::shared_ptr<QuantLib::MakeYoYInflationCapFloor> (copy->GetShared());
        
    }
}
Cephei::QL::Instruments::CMakeYoYInflationCapFloor::CMakeYoYInflationCapFloor (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeYoYInflationCapFloor = NULL;
#endif
	if (!t->IsSubclassOf(CMakeYoYInflationCapFloor::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CMakeYoYInflationCapFloor::CMakeYoYInflationCapFloor (QuantLib::Handle<QuantLib::MakeYoYInflationCapFloor>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMakeYoYInflationCapFloor = &childNative;
	_ppMakeYoYInflationCapFloor = &static_cast<boost::shared_ptr<QuantLib::MakeYoYInflationCapFloor>>(childNative.currentLink());
    
    _MakeYoYInflationCapFloorOwner = owner;
}
Cephei::QL::Instruments::CMakeYoYInflationCapFloor::CMakeYoYInflationCapFloor (QuantLib::Handle<QuantLib::MakeYoYInflationCapFloor> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMakeYoYInflationCapFloor = &childNative;
	_ppMakeYoYInflationCapFloor = &static_cast<boost::shared_ptr<QuantLib::MakeYoYInflationCapFloor>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CMakeYoYInflationCapFloor::CMakeYoYInflationCapFloor (QuantLib::MakeYoYInflationCapFloor childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeYoYInflationCapFloor = NULL;
#endif
	_ppMakeYoYInflationCapFloor = new boost::shared_ptr<QuantLib::MakeYoYInflationCapFloor> (new QuantLib::MakeYoYInflationCapFloor (childNative));
    
}
#endif

Cephei::QL::Instruments::CMakeYoYInflationCapFloor::~CMakeYoYInflationCapFloor ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMakeYoYInflationCapFloor != NULL)
    {
	    delete _ppMakeYoYInflationCapFloor;
        _ppMakeYoYInflationCapFloor = NULL;
    }
}
Cephei::QL::Instruments::CMakeYoYInflationCapFloor::!CMakeYoYInflationCapFloor ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMakeYoYInflationCapFloor != NULL)
    {
	    delete _ppMakeYoYInflationCapFloor;
    }
}
QuantLib::MakeYoYInflationCapFloor& Cephei::QL::Instruments::CMakeYoYInflationCapFloor::GetReference ()
{
    if (_ppMakeYoYInflationCapFloor == NULL) throw gcnew NativeNullException ();
	return **_ppMakeYoYInflationCapFloor;
}
boost::shared_ptr<QuantLib::MakeYoYInflationCapFloor>& Cephei::QL::Instruments::CMakeYoYInflationCapFloor::GetShared ()
{
    if (_ppMakeYoYInflationCapFloor == NULL) throw gcnew NativeNullException ();
	return *_ppMakeYoYInflationCapFloor;
}
QuantLib::MakeYoYInflationCapFloor* Cephei::QL::Instruments::CMakeYoYInflationCapFloor::GetPointer ()
{
    if (_ppMakeYoYInflationCapFloor == NULL) throw gcnew NativeNullException ();
	return &**_ppMakeYoYInflationCapFloor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MakeYoYInflationCapFloor>& Cephei::QL::Instruments::CMakeYoYInflationCapFloor::GetHandle ()
{
	if (_phMakeYoYInflationCapFloor == NULL)
	{
		_phMakeYoYInflationCapFloor = new Handle<QuantLib::MakeYoYInflationCapFloor> (*_ppMakeYoYInflationCapFloor);
	}
	return *_phMakeYoYInflationCapFloor;
}
#endif
bool Cephei::QL::Instruments::CMakeYoYInflationCapFloor::HasNative () 
{
	return (_ppMakeYoYInflationCapFloor != NULL);
}

Cephei::QL::Instruments::IMakeYoYInflationCapFloor^ Cephei::QL::Instruments::CMakeYoYInflationCapFloor::AsOptionlet (Microsoft::FSharp::Core::FSharpOption<Boolean>^ b)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _b = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (b) ? (bool)ValueHelper::Convert (b->Value) : true); //9a
    	QuantLib::MakeYoYInflationCapFloor& _rv = (QuantLib::MakeYoYInflationCapFloor&)(*_ppMakeYoYInflationCapFloor)->asOptionlet ( _b );   
        Cephei::QL::Instruments::CMakeYoYInflationCapFloor^ _nrv = gcnew Cephei::QL::Instruments::CMakeYoYInflationCapFloor (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IMakeYoYInflationCapFloor^ Cephei::QL::Instruments::CMakeYoYInflationCapFloor::WithEffectiveDate (DateTime effectiveDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _effectiveDate = (QuantLib::Date)ValueHelper::Convert (effectiveDate);
    	QuantLib::MakeYoYInflationCapFloor& _rv = (QuantLib::MakeYoYInflationCapFloor&)(*_ppMakeYoYInflationCapFloor)->withEffectiveDate ( _effectiveDate );   
        Cephei::QL::Instruments::CMakeYoYInflationCapFloor^ _nrv = gcnew Cephei::QL::Instruments::CMakeYoYInflationCapFloor (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IMakeYoYInflationCapFloor^ Cephei::QL::Instruments::CMakeYoYInflationCapFloor::WithFixingDays (UInt32 fixingDays)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays);
    	QuantLib::MakeYoYInflationCapFloor& _rv = (QuantLib::MakeYoYInflationCapFloor&)(*_ppMakeYoYInflationCapFloor)->withFixingDays ( _fixingDays );   
        Cephei::QL::Instruments::CMakeYoYInflationCapFloor^ _nrv = gcnew Cephei::QL::Instruments::CMakeYoYInflationCapFloor (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IMakeYoYInflationCapFloor^ Cephei::QL::Instruments::CMakeYoYInflationCapFloor::WithNominal (Double n)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _n = (QuantLib::Real)ValueHelper::Convert (n);
    	QuantLib::MakeYoYInflationCapFloor& _rv = (QuantLib::MakeYoYInflationCapFloor&)(*_ppMakeYoYInflationCapFloor)->withNominal ( _n );   
        Cephei::QL::Instruments::CMakeYoYInflationCapFloor^ _nrv = gcnew Cephei::QL::Instruments::CMakeYoYInflationCapFloor (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IMakeYoYInflationCapFloor^ Cephei::QL::Instruments::CMakeYoYInflationCapFloor::WithPaymentAdjustment (QL::Times::BusinessDayConventionEnum bdc)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
    	QuantLib::MakeYoYInflationCapFloor& _rv = (QuantLib::MakeYoYInflationCapFloor&)(*_ppMakeYoYInflationCapFloor)->withPaymentAdjustment ( _bdc );   
        Cephei::QL::Instruments::CMakeYoYInflationCapFloor^ _nrv = gcnew Cephei::QL::Instruments::CMakeYoYInflationCapFloor (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IMakeYoYInflationCapFloor^ Cephei::QL::Instruments::CMakeYoYInflationCapFloor::WithPaymentDayCounter (Cephei::QL::Times::IDayCounter^ dc)
{
    CDayCounter^ _Cdc;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
    	QuantLib::MakeYoYInflationCapFloor& _rv = (QuantLib::MakeYoYInflationCapFloor&)(*_ppMakeYoYInflationCapFloor)->withPaymentDayCounter ( _dc );   
        Cephei::QL::Instruments::CMakeYoYInflationCapFloor^ _nrv = gcnew Cephei::QL::Instruments::CMakeYoYInflationCapFloor (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Instruments::IMakeYoYInflationCapFloor^ Cephei::QL::Instruments::CMakeYoYInflationCapFloor::WithPricingEngine (Cephei::QL::IPricingEngine^ engine)
{
    CPricingEngine^ _Cengine;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cengine = safe_cast<CPricingEngine^> (engine);
        _Cengine->Lock();
        boost::shared_ptr<QuantLib::PricingEngine>& _engine = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_Cengine->GetShared ()); 
    	QuantLib::MakeYoYInflationCapFloor& _rv = (QuantLib::MakeYoYInflationCapFloor&)(*_ppMakeYoYInflationCapFloor)->withPricingEngine ( _engine );   
        Cephei::QL::Instruments::CMakeYoYInflationCapFloor^ _nrv = gcnew Cephei::QL::Instruments::CMakeYoYInflationCapFloor (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cengine != nullptr) _Cengine->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IMakeYoYInflationCapFloor^ Cephei::QL::Instruments::CMakeYoYInflationCapFloor_Factory::Create (QL::Instruments::YoYInflationCapFloor::TypeEnum capFloorType, UInt64 length, Cephei::QL::Times::ICalendar^ cal, Cephei::QL::Indexes::IYoYInflationIndex^ index, Cephei::QL::Times::IPeriod^ observationLag, Microsoft::FSharp::Core::FSharpOption<Double>^ strike, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IPeriod^>^ forwardStart)
{
    return gcnew CMakeYoYInflationCapFloor ( capFloorType,  length,  cal,  index,  observationLag,  strike,  forwardStart);
}
